package com.twu.biblioteca.controller;

import com.google.common.collect.Maps;
import com.google.inject.Inject;
import com.google.inject.Singleton;

import com.twu.biblioteca.entity.User;
import com.twu.biblioteca.enums.MapKey;
import com.twu.biblioteca.enums.Status;
import com.twu.biblioteca.model.UserModel;
import com.twu.biblioteca.view.command.Command;

import java.util.Map;

@Singleton
public class UserController extends BaseController {
  private final UserModel model;

  @Inject
  public UserController(UserModel model) {
    this.model = model;
  }

  public void checkCredential(String libraryNumber, String password) {
    Map<MapKey, Object> objectMap = Maps.newHashMap();

    boolean result = model.checkCredential(libraryNumber, password);

    if (result) {
      objectMap.put(MapKey.Command, Command.Type.Login);
      objectMap.put(MapKey.Status, Status.Success);
      objectMap.put(MapKey.User, model.findWithLibraryNumber(libraryNumber));
    } else {
      objectMap.put(MapKey.Command, Command.Type.Login);
      objectMap.put(MapKey.Status, Status.Failure);
    }

    update(objectMap);
  }

  public void getUserInfo(String libraryNumber) {
    User user = model.findWithLibraryNumber(libraryNumber);
    Map<MapKey, Object> objectMap = Maps.newHashMap();
    objectMap.put(MapKey.Command, Command.Type.UserInfo);
    objectMap.put(MapKey.Status, Status.Success);
    objectMap.put(MapKey.User, user);

    update(objectMap);
  }
}
